﻿using MvcApplication.Business;
using MvcApplication.Entity;
using MvcApplication.Interface;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Xml;

namespace MvcApplication.Controllers
{
    public class DisplayController : Controller
    {
        IWebDataService _webDataService;

        public DisplayController(IWebDataService webDataService)
        {
            _webDataService = webDataService;
        }

        public ActionResult Index()
        {
            var fileCachePath = DisplayBiz.GetIndexCachePath();

            if (!System.IO.File.Exists(fileCachePath))
            {
                var posts = _webDataService.GetPosts(new Pager(1, 24), new PostFilter { Status = PostStatus.发布 });
                var view = View("~/Views/Themes/Food/Index.cshtml", posts);

                var html = GetViewOutput(view);
                DisplayBiz.GenerationFileCache(html, fileCachePath);
                return null;
            }
            else
            {
                var html = System.IO.File.ReadAllText(fileCachePath);
                return Content(html);
            }
        }

        public ActionResult Detail(long postID)
        {
            var fileCachePath = DisplayBiz.GetFileCachePath(postID.ToString());

            if (!System.IO.File.Exists(fileCachePath))
            {
                var post = _webDataService.GetPost(postID);
                if (post == null) return HttpNotFound();
                var view = View("~/Views/Themes/Food/Detail.cshtml", post);
                var html = GetViewOutput(view);
                DisplayBiz.GenerationFileCache(html, fileCachePath);
                return null;
            }
            else
            {
                var html = System.IO.File.ReadAllText(fileCachePath);
                return Content(html);
            }

        }

        public ActionResult List(long id, Pager page, PostFilter filter)
        {
            var fileCachePath = DisplayBiz.GetCategoryCachePath(id.ToString(), page);
            if (!System.IO.File.Exists(fileCachePath))
            {
                var category = _webDataService.GetCategory(id);
                if (category == null) return HttpNotFound();

                filter.CategoryID = id;
                ViewBag.Posts = _webDataService.GetPosts(page, filter);
                var view = View("~/Views/Themes/Food/List.cshtml", category);
                var html = GetViewOutput(view);
                DisplayBiz.GenerationFileCache(html, fileCachePath);
                return null;
            }
            else
            {
                var html = System.IO.File.ReadAllText(fileCachePath);
                return Content(html);
            }
        }

        [HttpPost]
        public ActionResult Get(Pager page, PostFilter filter)
        {
            var jsoPath = DisplayBiz.GetPostListJsonPath(page, filter);
            if (System.IO.File.Exists(jsoPath))
            {
                var json = System.IO.File.ReadAllText(jsoPath);
                return Content(json, "application/json");
            }
            else
            {
                filter.Status = PostStatus.发布;
                var datas = _webDataService.GetPosts(page, filter);
                var json = Newtonsoft.Json.JsonConvert.SerializeObject(datas);
                DisplayBiz.GenerationFileCache(json, jsoPath);
                return Json(datas);
            }
        }

        public JsonResult Post(long id)
        {
            var post = _webDataService.GetPost(id);
            if (post.Status != PostStatus.发布)
                return Json(null, JsonRequestBehavior.AllowGet);

            return Json(post, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="hotNum">热门文章数量</param>
        /// <param name="topNum">顶置文章数量</param>
        /// <param name="recommendNum">推荐文章数量</param>
        /// <returns></returns>
        public ActionResult GetDisplayInfo(DisplayInfoFilter filter)
        {
            var jsoPath = DisplayBiz.GetDisplayInfoJsonPath(filter);
            if (System.IO.File.Exists(jsoPath))
            {
                var json = System.IO.File.ReadAllText(jsoPath);
                return Content(json, "application/json");
            }
            else
            {
                var data = _webDataService.GetDisplayInfo(filter);
                var json = Newtonsoft.Json.JsonConvert.SerializeObject(data);
                DisplayBiz.GenerationFileCache(json, jsoPath);
                return Json(data);
            }
        }

        string GetViewOutput(ViewResult view)
        {
            view.ExecuteResult(this.ControllerContext);

            using (var stream = new MemoryStream())
            {
                using (var writer = new StreamWriter(stream))
                {
                    var viewContext = new ViewContext(this.ControllerContext, view.View, view.ViewData, view.TempData, writer);

                    view.View.Render(viewContext, writer);
                    writer.Flush();

                    var bytes = stream.ToArray();
                    var html = System.Text.Encoding.UTF8.GetString(bytes);
                    return html;
                }
            }
        }
    }
}
